﻿package tools;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import java.util.Random;


public class CompareDemo  {

	public static void main(String[] args) {
		CompareDemo cd=new CompareDemo();
		ArrayList<MyComparable> alcomparable = cd.createComparableList();
		System.out.println("before sorted: "+alcomparable);
		Collections.sort(alcomparable);
		System.out.println("after sorted: "+alcomparable);
		MyComparable xmc=new MyComparable();
		xmc.setAge(25);
//		for(int i=0;i<alcomparable.size();i++){
//			if(xmc.compareTo(alcomparable.get(i))!=1){
//				alcomparable.add(i, xmc);
//				break;
//			}
//		}
		alcomparable.add(xmc);
		Collections.sort(alcomparable);
		System.out.println("after inserted: "+alcomparable);
		System.out.println("--------------------");
		ArrayList<MyComparator> alcomparator=cd.createComparatorList();
		System.out.println("before sorted: "+alcomparator);
		Collections.sort(alcomparator,new MyComparator());
		System.out.println("after sorted: "+alcomparator);
		MyComparator ymc=new MyComparator();
		ymc.setAge(25);
		alcomparator.add(ymc);
		Collections.sort(alcomparator,new MyComparator());
		System.out.println("after inserted: "+alcomparable);
	}
	public ArrayList<MyComparable> createComparableList(){
		ArrayList<MyComparable> al = new ArrayList<MyComparable>();
		for(int i=0;i<10;i++){
			MyComparable mc=new MyComparable();
			mc.setAge(new Random().nextInt(100));
			al.add(mc);
		}
		return al;
	}
	public ArrayList<MyComparator> createComparatorList(){
		ArrayList<MyComparator> al = new ArrayList<MyComparator>();
		for(int i=0;i<10;i++){
			MyComparator mc=new MyComparator();
			mc.setAge(new Random().nextInt(100));
			al.add(mc);
		}
		return al;
	}
}

class MyComparable implements Comparable<Object> {
	private int age;

	@Override
	public int compareTo(Object obj) {
		if (!(obj instanceof MyComparable)) {
			throw new ClassCastException();
		}
		MyComparable mc = (MyComparable) obj;
		return age>mc.age?1:age==mc.age?0:-1;
	}
	public String toString(){
		return String.valueOf(age);
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

class MyComparator implements Comparator<Object> {
	private int age;
	@Override
	public int compare(Object obj1, Object obj2) {
		if (!(obj1 instanceof MyComparator||obj2 instanceof MyComparator)) {
			throw new ClassCastException();
		}
		MyComparator mc1=(MyComparator)obj1;
		MyComparator mc2=(MyComparator)obj2;
		return mc1.age>mc2.age?1:mc1.age==mc2.age?0:-1;
	}
	public String toString(){
		return String.valueOf(age);
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

/*
class TestOne {

	public static void main(String[] args) throws IOException {
		Map<String, Integer> result = new HashMap<String, Integer>();
		InputStream ips = new FileInputStream("tmpfile\\txt\\info.txt");
		BufferedReader bfr = new BufferedReader(new InputStreamReader(ips));
		String line = bfr.readLine();
		while ((line = bfr.readLine()) != null) {
			dealLine(result, line);
		}
		showUser(result);
		sortUser(result);
	}

	public static void dealLine(Map<String, Integer> map, String line) {
		String results[] = line.split(",");
		if (results.length == 3) {
			String name = results[1];
			Integer num = (Integer) map.get(name);
			if (num == null)
				num = 0;
			map.put(name, ++num);
		}
	}

	public static void showUser(Map<String, Integer> map) {
		Set<String> set = map.keySet();
		Iterator<String> itr = set.iterator();
		while (itr.hasNext()) {
			String name = itr.next();
			Integer num = map.get(name);
			System.out.println("name: " + name + "num: " + num);
		}
	}

	public static void sortUser(Map<String, Integer> map) {
		Set<String> set = map.keySet();
		Iterator<String> itr = set.iterator();
		TreeSet<User> tset = new TreeSet<User>();
		while (itr.hasNext()) {
			String name = itr.next();
			Integer num = map.get(name);
			User user = new User(name, num);
			tset.add(user);
		}
		System.out.println(tset);
	}

	static class User implements Comparable<Object> {
		private Integer num = 0;
		private String name = null;
		
		public User() {
		}

		public User(String name, Integer num) {
			this.name = name;
			this.num = num;
		}

		public Integer getNum() {
			return num;
		}

		public void setNum(Integer num) {
			this.num = num;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		@Override
		public int compareTo(Object o) {
			User user = (User) o;
			if(this.num>user.num){return 1;}
			else if(this.num<user.num){return -1;}
			else{return this.name.compareTo(user.name);}
		}
		public String toString(){
			return "name: "+name+"num: "+num;
		}
	}
}
*/